package com.yb0os1.controller.admin;

import com.yb0os1.result.Result;
import com.yb0os1.serve.ReportServe;
import com.yb0os1.vo.OrderReportVO;
import com.yb0os1.vo.SalesTop10ReportVO;
import com.yb0os1.vo.TurnoverReportVO;
import com.yb0os1.vo.UserRepoertVO;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

@RestController
@RequestMapping("/admin/report")
@Slf4j
public class ReportController {
    @Autowired
    private ReportServe reportServe;

    @Operation(summary = "营业额统计接口")
    @GetMapping("/turnoverStatistics")
    public Result<TurnoverReportVO> turnoverStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("营业额数据统计{}-{}",begin,end);
        TurnoverReportVO turnoverReportVO = reportServe.turnoverStatistics(begin, end);
        log.info("营业额数据：{}",turnoverReportVO);
        return Result.success(turnoverReportVO);
    }


    @Operation(summary = "用户统计接口")
    @GetMapping("/userStatistics")
    public Result<UserRepoertVO> userStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("用户统计{} - {}",begin,end);
        UserRepoertVO userStatistics = reportServe.userStatistics(begin, end);
        log.info("营业额数据：{}",userStatistics);
        return Result.success(userStatistics);
    }

    @Operation(summary = "订单统计接口")
    @GetMapping("/ordersStatistics")
    public Result<OrderReportVO> ordersStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("订单统计接口{} - {}",begin,end);

        return Result.success(reportServe.ordersStatistics(begin,end));
    }

    @Operation(summary = "查询销量排名top10接口")
    @GetMapping("/top10")
    public Result<SalesTop10ReportVO> salesTop10(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("查询销量排名top10 {} - {}",begin,end);

        return Result.success(reportServe.getSalesTop10(begin,end));
    }

    @Operation(summary = "导出Excel报表接口")
    @GetMapping("/export")
    //HttpServletResponse获取输出流 将文件下载客户端浏览器
    public void export(HttpServletResponse response){
        reportServe.exportData(response);
    }
}
